(function() {
	var max_players = 0;

	function HandleNumPlayersChange(num_players) {
		for (var i = 0; i < num_players; i++) {
			$("#player" + i + "_fields").show();
		}
		for (var i = num_players; i < max_players; i++) {
			$("#player" + i + "_fields").hide();
		}
	}

	function UpdateNumPlayers(min_players, max_players) {
		var num_players = $("#num_players");
		num_players.empty();
		for (var i = min_players; i <= max_players; i++) {
			num_players.append("<option value='" + i + "'>" + i + "</option>");			
		}
		HandleNumPlayersChange(min_players);
	}

	function HandleGameChange(game) {
		var config = games[game];
		UpdateNumPlayers(config.min_players, config.max_players);
		if (config.has_ai) {
			$(".ai_fields").show();
		} else {
			$(".ai_fields").hide();
			// For change AI players, if any, to Email.
			$("input.ai_fields:checked").each(function(index, e) {
				var email = $("#" + e.id.replace(/_ai$/, "_email"));
				email.attr("checked", "checked");
			});
		}
  	}

	function HandlePlayerTypeChange(player, type) {
		var email = $("#player" + player + "_email");
		if (type == 'email') {
			email.attr("readonly", false);
		} else {
			email.attr("readonly", true);
			email.val("");
		}
	}

	function UpdateGame() {
    	// Sort games by name.
    	var sorted_games = [];
    	for (var game in games) {
      		sorted_games.push(game);
    	}
    	sorted_games.sort(function(a, b) {
      		if (games[a].name < games[b].name) {
        		return -1;
      		} else if (games[a].name > games[b].name) {
        		return 1;
      		} else {
        		return 0;
      		}
    	});

    	// Add all games to the drop-down menu.
    	for (var i in sorted_games) {
      		var game = sorted_games[i];
      		var config = games[game];

			if (config.max_players > max_players) {
				max_players = config.max_players;
			}

      		var s = config.name + " (";
      		if (config.min_players == config.max_players) {
        		s += config.max_players;
      		} else {
        		s += config.min_players + " to " + config.max_players;
      		}
      		if (config.max_players == 1) {
        		s += " player)";
			} else {
				s += " players)";
			}

			$("#game").append(
				"<option value='" + game + "'>" + s + "</option>");
		}
		
		// Add player fields.
		var players = $("#players");
		for (var i = 0; i < max_players; i++) {
			players.append("<span id='player" + i + "_fields'></span>");
			var fields = $('#player' + i + '_fields');
			fields.append("<label class='field_label'>Player " + (i + 1) + ":</label>");
			if (i == 0) {
				fields.append("<input type='text' name='player" + i +
				 "_email' value='" + email + "' readonly>");
			} else {
				var input_name = 'player' + i + '_email';
				fields.append("<input type='text' name='" + input_name +
					"' id='" + input_name + "'> ");

				// Add player type radio buttons
				input_name = 'player' + i + '_type';
				fields.append("<input type='radio' name='" + input_name +
					"' id='" + input_name + "_email' value='email' checked>");
				fields.append("<label for='" + input_name +
				    "_email'>Email</lable> ");
				fields.append("<input type='radio' name='" + input_name +
					"' id='" + input_name + "_public' value='public'>");
				fields.append("<label for='" + input_name +
				    "_public'>Public</lable> ");				
				fields.append("<input class='ai_fields' type='radio' name='" +
				 	input_name + "' id='" + input_name + "_ai' value='ai'>");
				fields.append("<label class='ai_fields' for='" + input_name +
				    "_ai'>AI</lable> ");
			}
			players.append("<br>");
		}

		// Select the first game, if available.
		if (sorted_games) {
			HandleGameChange(sorted_games[0]);			
		}
	}
	
	function SubmitCheck() {
		// Verify email addresses and check for duplicated players.
		var num_players = parseInt($("#num_players").val());
		var players = [];
		for (var i = 1; i < num_players; i++) {
			if ($("#player" + i + "_type_email").is(":checked")) {
				var email = $.trim($("#player" + i + "_email").val());
				if (email == "") {
					alert("You must set email for player " + (i + 1) + ".");
					return false;
				}
				if (email.indexOf("@") == -1) {
					alert(email + " is invalid.");
					return false;
				}
				for (var j = 0; j < players.length; j++) {
					if (players[j] == email) {
						alert(email + " has been specified more than once.");
						return false;
					}
				}
				players.push(email);
			}
		}
		return true;
	}
	
  	$(document).ready(function() {
		UpdateGame();
		common.AutoSelectTextFields();

		$("#game").change(function(e) {
			 HandleGameChange(e.target.value);
		});
		$("#num_players").change(function(e) {
			HandleNumPlayersChange(e.target.value);
		});
		$("input[type=radio]").change(function(e) {
			var player = parseInt(e.target.name.match(/player(\d+)_type/)[1]);
			HandlePlayerTypeChange(player, e.target.value);
		});
		$("#submit").click(function(e) {
			if (!SubmitCheck()) {
				e.preventDefault();
			}
		});
	});
}());
